package dyy.zc.sportruler.views;

import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.drawable.ColorDrawable;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.widget.ImageView;
import android.widget.PopupWindow;
import android.widget.RelativeLayout;
import android.widget.TextView;

import dyy.zc.sportruler.R;
import dyy.zc.sportruler.ui.activity.MainActivity;
import dyy.zc.sportruler.ui.activity.MakePlanActivity;
import dyy.zc.sportruler.utils.AppManager;
import dyy.zc.sportruler.utils.Utils;

/**
 * 底部弹窗2  联系商家
 * Created by zhouchuan on 2017/3/15.
 */

public class CodePopWindows extends PopupWindow {
    public View view;
    private Context context;
    private Bitmap bitmap;
    private TextView name;
    private ImageView code,close;
    private String planName;

    public String getPlanName() {
        return planName;
    }

    public void setPlanName(String planName) {
        this.planName = planName;
    }

    public void setBitmap(Bitmap bitmap) {
        this.bitmap = bitmap;
        code.setImageBitmap(bitmap);
    }

    public CodePopWindows(Context context){
        view = LayoutInflater.from(context).inflate(R.layout.pop_code,null);
        this.context = context;
        findViewsByIds(view);
        bind();
          /* 设置弹出窗口特征 */
        // 设置视图
        this.setContentView(this.view);
        // 设置弹出窗体的宽和高
//        this.setHeight(110*3+26);
        this.setHeight(RelativeLayout.LayoutParams.WRAP_CONTENT);
        this.setWidth(RelativeLayout.LayoutParams.WRAP_CONTENT);
//        this.setWidth(width/3-40);
        // 设置弹出窗体可点击
        this.setFocusable(true);
        this.setOutsideTouchable(false);
        // 实例化一个ColorDrawable颜色为半透明
//        ColorDrawable dw = new ColorDrawable(0x00000000);
        // 设置弹出窗体的背景
//        this.setBackgroundDrawable(dw);
//        this.setTouchInterceptor(new View.OnTouchListener() {
//
//            @Override
//            public boolean onTouch(View v, MotionEvent event) {
//                // 如果点击了popupwindow的外部，popupwindow也会消失
//                // 这里如果返回true的话，touch事件将被拦截
//                // 拦截后 PopupWindow的onTouchEvent不被调用，这样点击外部区域无法dismiss
//                if (event.getAction() == MotionEvent.ACTION_OUTSIDE) {
//                    //popupMenu.dismiss();
////                    StationEventCaptain.getInstance().fireEventDataChange(
////                            new StationEventData(IStationEventName.HEDGE_KEYBORAD_DIMISS, true));
//                    return true;
//                }
//                return false;
//            }
//        });
        // 设置弹出窗体显示时的动画，从底部向上弹出
        this.setAnimationStyle(R.style.xunleiDialogAnimation);
    }

    public void initData(){
        name.setText(getPlanName());
    }

    private void bind() {
        close.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                dismiss();
                context.startActivity(new Intent(context, MainActivity.class));
                AppManager.getInstance().finishOtherActivity();
            }
        });
//        code.setImageBitmap(bitmap);
    }

    private void findViewsByIds(View view) {
        name = Utils.findViewsById(view,R.id.name);
        code = Utils.findViewsById(view,R.id.code_img);
        close = Utils.findViewsById(view,R.id.close);
    }
}
